package com.jmuok.web.fff.web;

import java.util.Locale;

import com.jmuok.web.fff.web.vaadin.BaseApplication;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.Runo;

public class MainApplication extends BaseApplication {

	private static final long serialVersionUID = 1L;

	@Override
	public void init() {
		setMainWindow(new Window(helper.getMessage("webapp.name")));
		setTheme(Runo.themeName());
		VerticalLayout root = new VerticalLayout();
		getMainWindow().setContent(root);
		root.setSizeFull();
		ComboBox localeBox = initLocaleBox();

		FormLayout localeLayout = new FormLayout();
		localeLayout.setMargin(false);
		localeLayout.setSpacing(false);
		localeLayout.setSizeUndefined();
		localeLayout.addComponent(localeBox);
		root.addComponent(localeLayout);
		root.setComponentAlignment(localeLayout, Alignment.TOP_RIGHT);

		Label title = new Label(helper.getMessage("webapp.name"));
		title.addStyleName(Runo.LABEL_H1);
		title.setSizeUndefined();
		root.addComponent(title);
		root.setExpandRatio(title, 1);
		root.setComponentAlignment(title, Alignment.BOTTOM_CENTER);

		Label slogan = new Label(helper.getMessage("webapp.slogan"));
		slogan.addStyleName(Runo.LABEL_SMALL);
		slogan.setSizeUndefined();
		root.addComponent(slogan);
		root.setComponentAlignment(slogan, Alignment.BOTTOM_CENTER);

		Label spacer = new Label("");
		spacer.setHeight("20px");
		root.addComponent(spacer);


	}

	private ComboBox initLocaleBox() {
		Locale[] supportedLocals = { Locale.SIMPLIFIED_CHINESE, Locale.JAPAN };
		String[] sNames = { "cn", "jp" };
		ComboBox localeBox = new ComboBox(helper.getMessage("label.language"));
		localeBox.setNullSelectionAllowed(false);
		localeBox.setImmediate(true);
		localeBox.setTextInputAllowed(false);
		localeBox.setWidth("140px");

		for (int i = 0; i < supportedLocals.length; i++) {
			localeBox.addItem(supportedLocals[i]);
			localeBox.setItemCaption(supportedLocals[i],
					supportedLocals[i].getDisplayName(supportedLocals[i]));
			localeBox.setItemIcon(supportedLocals[i], new ThemeResource(
					"../web/flags/" + sNames[i] + ".gif"));
			// Automatically select the current locale
			if (supportedLocals[i].equals(getLocale()))
				localeBox.setValue(supportedLocals[i]);
		}
		localeBox.addListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				setLocale((Locale) event.getProperty().getValue());
				close();
			}
		});
		return localeBox;
	}

}
